package com.fernandomartines.dbcheck.util;

import java.text.DecimalFormat;

public class StringUtil {

    private StringUtil() {
        
    }

    public static int convertStringToIntDef(String s, int defaultValue) {
        int result = defaultValue;
        try {
            result = Integer.parseInt(s);
        } catch (Exception e) {
            result = defaultValue;
        }
        return result;
    }

    public static String replicateString(String s, String replicate, int len) {
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < (len - s.length()); i++)
            sb.append(replicate);
        return sb.toString();
    }

    public static String applyZeroString(String s, int len) {
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < (len - s.length()); i++)
            sb.insert(0, "0");
        return sb.toString();
    }

    public static String removeString(String s, String remove) {
        while (s.contains(remove))
            s = s.replaceAll(remove, "");
        return s.toString();
    }

    public static String formatNumber(float f, String format) {
        return new DecimalFormat(format).format(f);
    }

    public static String formatNumber(int f, String format) {
        return new DecimalFormat(format).format(f);
    }

}
